package cn.hchaojie.javase.day08;

import static org.junit.Assert.assertArrayEquals;

import java.util.Arrays;
import java.util.Comparator;

import org.junit.Test;

public class TestSort2 {
	// 按照字符串长度进行排序（自定义的规则）
	@Test
	public void test3() {
		String[] array = {"ab", "abc", "bc", "de", "bb"};
		
		String[] expect = {"ab", "bc", "de", "bb", "abc"};
		
		// 排序
		// Arrays.sort(array /*待排序的数组*/, /*比较器*/);
		
		Arrays.sort(array, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return o1.length() - o2.length();
			}
		});
		
		// 使用lambda表达式简化匿名内部类
		// 参数 -> {}
		Arrays.sort(array, (o1, o2) -> {
			return o1.length() - o2.length();
		});
		
		System.out.println(Arrays.toString(array));
		
		assertArrayEquals(array, expect);
	}
	
	@Test
	public void test5() throws InterruptedException {
		// Thread t = new Thread(new Task());
		
//		Thread t = new Thread(new Runnable() {
//			@Override
//			public void run() {
//				System.out.println("...");
//			}
//		});
		
		Thread t = new Thread(() -> {
			System.out.println("...");
		});
		
		t.start();
		
		Thread.sleep(1000);
	}
	
	class Task implements Runnable {
		@Override
		public void run() {
			System.out.println("。。。");
		}
	}
	
	// 按照字符串长度进行排序（自定义的规则）
	// 字符串长的放到前面（跟测试3顺序相反）
	@Test
	public void test4() {
		String[] array = { "ab", "abc", "bc", "de", "bb" };

		String[] expect = {"abc", "ab", "bc", "de", "bb"};

		// 排序
		// Arrays.sort(array /*待排序的数组*/, /*比较器*/);

		Arrays.sort(array, new StringReverseComparator());

		System.out.println(Arrays.toString(array));

		assertArrayEquals(array, expect);
	}
}
